﻿@model Peninsula.DataAccess.POCO.Role

@{
    bool isEdit = ViewBag.IsEdit;
    string title = isEdit ? "编辑" : "创建";
}

@section Styles{
    <style type="text/css">
    </style>
}
@section Scripts{
    <script type="text/javascript">

        $(function () {
            function getTree() {
                var tree = @Html.Raw(ViewBag.PermissionJsonTree);
                return tree;
            }

            $('#permissionTree').treeview({
                data: getTree(),
                showCheckbox: true,
                highlightSelected: false,
                //递归选择父节点
                onNodeChecked: function (event, data) {
                    var that = this;
                    function checkParentNode(note){
                        if (note.parentId !==undefined) {
                            var parentNote = $(that).treeview('getNode', note.parentId);
                            $(that).treeview('checkNode', parentNote.nodeId);
                            checkParentNode(parentNote);
                        }
                    }
                    checkParentNode(data);
                },
                //递归取消子节点选择
                onNodeUnchecked: function (event, data) {
                    var that = this;
                    function uncheckChindrenNode(nodes) {
                        if (nodes && nodes.length) {
                            $.each(nodes, function (i, v) {
                                $(that).treeview('uncheckNode', v.nodeId);
                                uncheckChindrenNode(v.nodes);
                            });
                        }
                    }
                    uncheckChindrenNode(data.nodes);
                }
            });


            window.beforeAjaxFormSubmit = function(){
                debugger
                var selectPermissionIds = [];
                var selectNode = $('#permissionTree').treeview('getChecked');
                for(var i = 0; i< selectNode.length; i++){
                    selectPermissionIds.push(selectNode[i].serverId);
                }
                $("#PermissionId").val(selectPermissionIds.join(","));
            }
        });
    </script>
}


<!-- BEGIN FORM PORTLET -->
<div class="portlet">
    <div class="portlet-title">
        <div class="caption">
            <i class="fa fa-reorder"></i>@(title)角色
        </div>
    </div>
    <div class="portlet-body form">

        @using (Html.BeginForm("CreateOrEditRole", "Account", FormMethod.Post,
                        new { @class = "form-horizontal js-form js-ajax-form" }))
        {
            @Html.AntiForgeryToken()
            @Html.HiddenFor(m => m.Id)
            @Html.Hidden("isEdit", isEdit)
            <div class="form-body">

                <div class="form-group">
                    <label class="col-md-3 control-label">
                        <span style="color:red">*</span> @Html.DisplayNameFor(m => m.RoleName)
                    </label>
                    <div class="col-md-7">
                        @Html.TextBoxFor(m => m.RoleName, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.RoleName)
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">
                        @Html.DisplayNameFor(m => m.Description)
                    </label>
                    <div class="col-md-7">
                        @Html.TextAreaFor(m => m.Description, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Description)
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">
                         @Html.DisplayNameFor(m => m.Permissions)
                    </label>
                    <div class="col-md-7">
                        <div id="permissionTree"></div>
                        @Html.Hidden("PermissionId")
                    </div>
                </div>
                @if (isEdit)
                {
                    <div class="form-group">
                        <label class="col-md-3 control-label">
                            @Html.DisplayNameFor(m => m.Admins)
                        </label>
                        <div class="col-md-7">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>
                                                用户名
                                            </th>
                                            <th>
                                                真实姓名
                                            </th>
                                            <th>
                                                操作
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach (var admin in Model.Admins)
                                        {
                                            <tr>
                                                <td>@Html.DisplayFor(item => admin.UserName)</td>
                                                <td>@Html.DisplayFor(item => admin.TrueName)</td>
                                                <td>
                                                    <a href="@Url.Action("CreateOrEditAdmin","Account",new { id = admin.Id })" class="btn btn-info btn-xs"><i class="fa fa-edit"></i>编辑</a>
                                                    <a data-confirm-text="确定要移除[@(admin.TrueName)]的[@(Model.RoleName)]角色吗？" data-confirm-button="确定" data-cancel-button="取消"
                                                       data-confirmed-url="@Url.Action("DeleteAdminRole", "Account", new { adminId = admin.Id, roleId = Model.Id })"
                                                       class="btn btn-danger btn-xs js-table-ajax-confirm-button">
                                                        <i class="fa fa-trash-o"></i>移除角色
                                                    </a>
                                                </td>
                                            </tr>
                                        }
                                    </tbody>
                                </table>
                            </div>

                        </div>
                    </div>
                }
                <div class="form-group">
                    <div class="col-md-offset-3 col-md-7">
                        @Html.ValidationSummary()
                    </div>
                </div>
            </div>
            <div class="form-actions fluid">
                <div class="col-md-offset-3 col-md-9">
                    <button type="submit" class="btn btn-success">保存</button>
                    <a href="@HttpContext.Current.Request.UrlReferrer" class="btn btn-default">取消</a>
                </div>
            </div>
        }
    </div>
</div>